package com.sxfq.loansupermarket.backsystem.modular.logsf.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.Dept;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.PayServicedInfo;
import com.sxfq.loansupermarket.backsystem.modular.logsf.dao.DiscountAuditDao;
import com.sxfq.loansupermarket.backsystem.modular.logsf.entity.DiscountAudit;
import com.sxfq.loansupermarket.backsystem.modular.logsf.service.IDiscountExamineService;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.TongJiInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DiscountAuditServiceImpl implements IDiscountExamineService {
    @Autowired
    private DiscountAuditDao dao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<DiscountAudit> getList(Page<DiscountAudit> page, DiscountAudit searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getOutPatientName())) {
            if ((redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"DeptName",0)+"").indexOf("总公司") == -1) {
                params.put("outPatientName", ShiroKit.getUser().getOutpatientName());
            } else {
                params.put("outPatientName", searchInfo.getOutPatientName());
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getAuditStatus())) {
            params.put("auditStatus", searchInfo.getAuditStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartCreateTime())) {
            params.put("startCreateTime", searchInfo.getStartCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndCreateTime())) {
            params.put("endCreateTime", searchInfo.getEndCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getAuditNode())) {
            params.put("auditNode", searchInfo.getAuditNode());
        }
        if (StringUtil.isNotEmpty(searchInfo.getAuditStatusValue())) {
            if (searchInfo.getAuditStatusValue() == 0) {
                if (Integer.parseInt(redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"AuditNode",0)+"") == 1) {
                    params.put("status", " t.status =0");
                } else if (Integer.parseInt(redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"AuditNode",0)+"") == 2) {
                    params.put("status", " t.status =1");
                } else {
                    params.put("status", " t.status =3");
                }
            } else {
                if (Integer.parseInt(redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"AuditNode",0)+"") == 1) {
                    params.put("status", " (t.status =1 or t.status =2)");
                } else if (Integer.parseInt(redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"AuditNode",0)+"") == 2) {
                    params.put("status", " t.status =2");
                } else {
                    params.put("status", " t.status =3");
                }
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getUserDept())) {
            params.put("userDept", searchInfo.getUserDept());
        }
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getList(page, params);
    }

    @Override
    public int getQueryPagesCount(Page<DiscountAudit> page, DiscountAudit searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getOutPatientName())) {
            if ((redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"DeptName",0)+"").indexOf("总公司") == -1) {
                params.put("outPatientName", ShiroKit.getUser().getOutpatientName());
            } else {
                params.put("outPatientName", searchInfo.getOutPatientName());
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getAuditStatus())) {
            params.put("auditStatus", searchInfo.getAuditStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartCreateTime())) {
            params.put("startCreateTime", searchInfo.getStartCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndCreateTime())) {
            params.put("endCreateTime", searchInfo.getEndCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getAuditNode())) {
            params.put("auditNode", searchInfo.getAuditNode());
        }
        if (StringUtil.isNotEmpty(searchInfo.getAuditStatusValue())) {
            if (searchInfo.getAuditStatusValue() == 0) {
                if (Integer.parseInt(redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"AuditNode",0)+"") == 1) {
                    params.put("status", " t.status =0");
                } else if (Integer.parseInt(redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"AuditNode",0)+"") == 2) {
                    params.put("status", " t.status =1");
                } else {
                    params.put("status", " t.status =3");
                }
            } else {
                if (Integer.parseInt(redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"AuditNode",0)+"") == 1) {
                    params.put("status", " (t.status =1 or t.status =2)");
                } else if (Integer.parseInt(redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"AuditNode",0)+"") == 2) {
                    params.put("status", " t.status =2");
                } else {
                    params.put("status", " t.status =3");
                }
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getUserDept())) {
            params.put("userDept", searchInfo.getUserDept());
        }
        return dao.getQueryPagesCount(page, params);
    }

    @Override
    public List<TongJiInfo> getQueryPagesCounts(DiscountAudit searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(searchInfo.getOutPatientName())) {
            if ((redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"DeptName",0)+"").indexOf("总公司") == -1) {
                params.put("outPatientName", ShiroKit.getUser().getOutpatientName());
            } else {
                params.put("outPatientName", searchInfo.getOutPatientName());
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getAuditStatus())) {
            params.put("auditStatus", searchInfo.getAuditStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartCreateTime())) {
            params.put("startCreateTime", searchInfo.getStartCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndCreateTime())) {
            params.put("endCreateTime", searchInfo.getEndCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getAuditNode())) {
            params.put("auditNode", searchInfo.getAuditNode());
        }
        if (Integer.parseInt(redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"AuditNode",0)+"") == 1) {
            params.put("status", " t.status =0");
            params.put("status1", " (t.status =1 or t.status =2)");
        } else if (Integer.parseInt(redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"AuditNode",0)+"") == 2) {
            params.put("status", " t.status =1");
            params.put("status1", " t.status =2");
        } else {
            params.put("status", " t.status =3");
            params.put("status1", " t.status =3");
        }
        if (StringUtil.isNotEmpty(searchInfo.getUserDept())) {
            params.put("userDept", searchInfo.getUserDept());
        }
        return dao.getQueryPagesCounts(params);
    }

    @Override
    public int updateAuditStatus(DiscountAudit info) {
        Double discount = info.getDiscount();
        int levelTwoAudit = Integer.parseInt(redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"LevelTwoAudit",0)+"");
        if (discount >= levelTwoAudit) {
            info.setAuditUser1(ShiroKit.getUser().getAccount());
            info.setAuditTime1(new Date());
            String id = info.getId();
            String sql = "select * from t_discount_audit where id = '" + id + "'";
            DiscountAudit discountAudit = sqlMapper.selectOne(sql, DiscountAudit.class);
            String dept = discountAudit.getOutPatientName();
            String deptSql = "select * from dept where simplename = '" + dept + "'";
            Dept dp = sqlMapper.selectOne(deptSql, Dept.class);
            String tableName = dp.getAbbreviation() + "deal_info";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dealSql = "select * from " + tableName + " where reservations_id = '" + discountAudit.getReservationsId() + "' and operation_time " +
                    "= '" + sdf.format(discountAudit.getOperationTime()) + "' and customer_id = '" + discountAudit.getCustomerId() + "'";
            List<PayServicedInfo> list = sqlMapper.selectList(dealSql, PayServicedInfo.class);
            if (list.size() > 0) {
                String dealStatus = list.get(0).getDealStatus();
                if ("待审核".equals(dealStatus)) {
                    String updateSql = "update " + tableName + " set deal_status = '已开单' where reservations_id = '" + discountAudit.getReservationsId() + "' and operation_time " +
                            "= '" + sdf.format(discountAudit.getOperationTime()) + "' and customer_id = '" + discountAudit.getCustomerId() + "'";
                    sqlMapper.update(updateSql);
                }
            }
        } else {
            String status = info.getStatus();
            if ("1".equals(status)) {
                info.setAuditUser1(ShiroKit.getUser().getAccount());
                info.setAuditTime1(new Date());
            } else if ("2".equals(status)) {
                info.setAuditUser2(ShiroKit.getUser().getAccount());
                info.setAuditTime2(new Date());
                String id = info.getId();
                String sql = "select * from t_discount_audit where id = '" + id + "'";
                DiscountAudit discountAudit = sqlMapper.selectOne(sql, DiscountAudit.class);
                String dept = discountAudit.getOutPatientName();
                String deptSql = "select * from dept where simplename = '" + dept + "'";
                Dept dp = sqlMapper.selectOne(deptSql, Dept.class);
                String tableName = dp.getAbbreviation() + "deal_info";
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dealSql = "select * from " + tableName + " where reservations_id = '" + discountAudit.getReservationsId() + "' and operation_time " +
                        "= '" + sdf.format(discountAudit.getOperationTime()) + "' and customer_id = '" + discountAudit.getCustomerId() + "'";
                List<PayServicedInfo> list = sqlMapper.selectList(dealSql, PayServicedInfo.class);
                if (list.size() > 0) {
                    String dealStatus = list.get(0).getDealStatus();
                    if ("待审核".equals(dealStatus)) {
                        String updateSql = "update " + tableName + " set deal_status = '已开单' where reservations_id = '" + discountAudit.getReservationsId() + "' and operation_time " +
                                "= '" + sdf.format(discountAudit.getOperationTime()) + "' and customer_id = '" + discountAudit.getCustomerId() + "'";
                        sqlMapper.update(updateSql);
                    }
                }
            }
        }
        return dao.updateAuditStatus(info);
    }

    @Override
    public int auditFailed(DiscountAudit info) {
        String id = info.getId();
        String sql = "select * from t_discount_audit where id = '" + id + "'";
        DiscountAudit discountAudit = sqlMapper.selectOne(sql, DiscountAudit.class);
        String dept = discountAudit.getOutPatientName();
        String deptSql = "select * from dept where simplename = '" + dept + "'";
        Dept dp = sqlMapper.selectOne(deptSql, Dept.class);
        String tableName = dp.getAbbreviation() + "deal_info";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dealSql = "select * from " + tableName + " where reservations_id = '" + discountAudit.getReservationsId() + "' and operation_time " +
                "= '" + sdf.format(discountAudit.getOperationTime()) + "' and customer_id = '" + discountAudit.getCustomerId() + "'";
        List<PayServicedInfo> list = sqlMapper.selectList(dealSql, PayServicedInfo.class);
        if (list.size() > 0) {
            String dealStatus = list.get(0).getDealStatus();
            if ("待审核".equals(dealStatus)) {
                String updateSql = "update " + tableName + " set deal_status = '审核未通过' where reservations_id = '" + discountAudit.getReservationsId() + "' and operation_time " +
                        "= '" + sdf.format(discountAudit.getOperationTime()) + "' and customer_id = '" + discountAudit.getCustomerId() + "'";
                sqlMapper.update(updateSql);
            }
        }
        String status = info.getStatus();
        if ("0".equals(status)) {
            info.setAuditUser1(ShiroKit.getUser().getAccount());
            info.setAuditTime1(new Date());
            info.setStatus("2");
        } else {
            info.setAuditUser2(ShiroKit.getUser().getAccount());
            info.setAuditTime2(new Date());
            info.setStatus("2");
        }
        return dao.updateAuditStatus(info);
    }
}